/*
 ============================================================================
 Name		: fReaderAppView.cpp
 Author	  : wangchao
 Copyright   : Your copyright notice
 Description : Application view implementation
 ============================================================================
 */

// INCLUDE FILES
#include <coemain.h>
#include "fReaderAppView.h"
#include "fReaderWidget.h"
#include "AllocTemplate.h"
#include "fWidgetContent.h"
// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CfReaderAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CfReaderAppView* CfReaderAppView::NewL( const TRect& aRect )
{
    CfReaderAppView* self = CfReaderAppView::NewLC( aRect );
    CleanupStack::Pop( self );
    return self;
}

// -----------------------------------------------------------------------------
// CfReaderAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CfReaderAppView* CfReaderAppView::NewLC( const TRect& aRect )
{
    CfReaderAppView* self = new ( ELeave ) CfReaderAppView;
    CleanupStack::PushL( self );
    self->ConstructL( aRect );
    return self;
}

// -----------------------------------------------------------------------------
// CfReaderAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CfReaderAppView::ConstructL( const TRect& aRect )
{
    // Create a window for this application view
    CreateWindowL( );
    
    // Set the windows size
    SetRect( aRect );
    EnableDragEvents( );
    // _widget = AllocNoConstructL0< fReaderWidget >::NewL( );// fReaderWidget::NewL( aRect, this );
    // _widget->ConstructL( aRect, this );

    _widget = Constructor2< fReaderWidget, const TRect&, fWidgetContainer* >::NewL( aRect, this );
    /*CEikonEnv::Static( )*/
    //iCoeEnv->AddForegroundObserverL( *this );
    // Activate the window, which makes it ready to be drawn
    ActivateL( );
}

// -----------------------------------------------------------------------------
// CfReaderAppView::CfReaderAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CfReaderAppView::CfReaderAppView( )
{
    // No implementation required
}

// -----------------------------------------------------------------------------
// CfReaderAppView::~CfReaderAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CfReaderAppView::~CfReaderAppView( )
{
    // No implementation required
}

// -----------------------------------------------------------------------------
// CfReaderAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CfReaderAppView::Draw( const TRect& /*aRect*/) const
{
    CWindowGc & gc = SystemGc( );
    if( _widget )
    {
        fWidgetContent* content = _widget->content( );
        //TRect area = _widget->InvalidRect( );
        gc.BitBlt( content->area( ).iTl, content->data( ), content->area( ) );
    }
    else
    {
        gc.Clear( );
    }
}
void CfReaderAppView::WidgetContentChanged( )
{
    DrawNow( );
}
// -----------------------------------------------------------------------------
// CfReaderAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CfReaderAppView::SizeChanged( )
{
    if( _widget )
    {
        _widget->sizeChanged( this->Size( ) );
    }
}

// -----------------------------------------------------------------------------
// CfReaderAppView::HandlePointerEventL()
// Called by framework to handle pointer touch events.
// Note: although this method is compatible with earlier SDKs, 
// it will not be called in SDKs without Touch support.
// -----------------------------------------------------------------------------
//
void CfReaderAppView::HandlePointerEventL( const TPointerEvent& aPointerEvent )
{
    /*for( TInt i = 0; i < iCtrlArray.Count( ); i++ )
     {
     CCoeControl* temp = iCtrlArray[ i ];
     if( temp->IsFocused( ) )
     {
     temp->HandlePointerEventL( aPointerEvent );
     }
     }*/
    _widget->handlePointEvent( fPointEvent( aPointerEvent ) );
    // Call base class HandlePointerEventL()
    //CCoeControl::HandlePointerEventL( aPointerEvent );
}
void CfReaderAppView::HandleGainingForeground( )
{
    _widget->resume( );
    //_widget->ForegroundEvent( ETrue );
}
void CfReaderAppView::HandleLosingForeground( )
{
    _widget->pause( );
    //_widget->ForegroundEvent( EFalse );
}

fReaderWidget* CfReaderAppView::Widget( )
{
    return _widget;
}

// End of File
